define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'exam/index' + location.search,
                    add_url: 'exam/add',
                    edit_url: 'exam/edit',
                    del_url: 'exam/del',
                    dragsort_url: 'ajax/weigh', 
                    multi_url: 'exam/multi',
                    table: 'exam',
                    auto_add:'exam/auto_add',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'ctitle', title: __('Ctitle'),formatter:function(value,row,index){
                                value=value?value:'';
                                var length=value.length;
                                if(length&&length>7){
                                   length=20;
                                   return"<span title ='"+value+"'>"+value.substring(0,length)+"...</span>"
                                }
                                return"<span title ='"+value+"'>"+value.substring(0,length)+"</span>"
                                
                            }
                        },
                        {field: 'csubjecttype', title: __('Csubjecttype'), searchList: {"1":__('Csubjecttype 1'),"2":__('Csubjecttype 2'),"3":__('Csubjecttype 3'),"4":__('Csubjecttype 4')}, formatter: Table.api.formatter.normal},
                        {field: 'canswer', title: __('Canswer')},
                        {field: 'iscore', title: __('Iscore'), operate:'BETWEEN'},
                        {field: 'cdescription', title: __('Cdescription'),formatter:function(value,row,index){
                                value=value?value:'';
                                var length=value.length;
                                if(length&&length>7){
                                   length=30;
                                   return"<span title ='"+value+"'>"+value.substring(0,length)+"...</span>"
                                }
                                return"<span title ='"+value+"'>"+value.substring(0,length)+"</span>"
                                
                            }
                        },
                        {field: 'chapter.cchaptername', title: '章节名称'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        auto_add: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});